<?php
include 'AdminController.php';
class Admin_RoomController extends Admin_AdminController
{
    public function init(){
    	//レイアウトを設置する
        $layoutPath = APPLICATION_PATH . '/templates/admin/admin';
        $option = array('layout' => 'index', 'layoutPath' => $layoutPath);
        Zend_Layout::startMvc($option);
        
        //アクティブメニューを設定
        $this->view->active_menu = "rooms";
    }
	
	public function indexAction ()
    {
    	//TODO 必要なクラスを読み込む
    	Zend_Loader::loadClass("RoomModel");
    	Zend_Loader::loadClass("TableModel");
    	Zend_Loader::loadClass("SelectListModel");
    	
    	$request = $this->getRequest();
    	$view = $this->view;
    	
    	//TODO パラメーター取る
    	$search_type = $request->getParam('search_type', "all");
    	$search_key = $request->getParam('search_key');
    	$page = $request->getParam('page', 1);
    	$number_seat = $request->getParam("number_seat", 0);
    	$type = $request->getParam("type", 0);
    	
    	//TODO　でーたを読み出す 
    	$model = new RoomModel();
    	$data = $model->getListRoom($search_type, $search_key, $number_seat, $type, $page, 10);
    	$data_count = $model->getListRoomCount($search_type, $search_key, $number_seat, $type);
    	
    	//TODO データを一覧に表現するためにテーブルモデルを作成
    	$table_model = new TableModel();
    	$table_model->setTable_title("会議室");
    	
    	//TODO テーブルのヘッダーを設定
    	$table_model->addHeader('code', 'コード', true);
    	$table_model->addHeader('type', '会議室タイプ', false);
    	$table_model->addHeader('number_seat', '席の数', true);
    	$table_model->addHeader('rent_price', '単価(/３０分）', false);
    	$table_model->addHeader('description', '概要', false);

    	//TODO テーブルのデータを設定
    	$table_model->setTable_data($data);
    	$table_model->setData_count($data_count);
    	$table_model->setPage($page);
    	$table_model->setLine_per_page(10);

    	//TODO テーブルのリンクを設定
    	$table_model->setSearch_link("/admin/room");
    	$table_model->setAdd_link("/admin/room/add");
    	$table_model->setEdit_link("/admin/room/edit");
    	$table_model->setDelete_link("/admin/room/delete");
    	
    	//TODO　ビューに送る
    	$view->table = $table_model;
    	
    	//TODO　テーブルのデータをトランスレートする
    	$type_stranslate = array(
    		1 => '会議室',
    		2 => '会議ブース'
    	);
    	$table_translate['type'] = $type_stranslate;
    	$view->table_translate = $table_translate;
    	
    	//TODO　AJAXで検索場合
    	if($request->getParam('search')){
    		//disable layout
    		$layout = $this->_helper->layout();
		    $layout->disableLayout();
		    
		    $view->search_mode = 1;
    	}
    	
    	//TODO notify
    	$view->msg = $request->getParam('msg');
    	
    	//TODO　必要なデータをビューに送る
    	$view->room_type_list = SelectListModel::getListRoomType(true);
    	$view->number_seat_list = SelectListModel::getListSeatNumberList(true);
    	
    	//TODO Navigator を設定
    	$nav['ホーム'] = "/admin";
    	$nav['会議室'] = "/admin/room";
    	$view->nav = $nav;
    }
    
    /**
     * 会議室を追加するためのアクション
     */
    public function addAction(){
    	//TODO　必要クラスを読み込む
    	Zend_Loader::loadClass("RoomModel");
    	Zend_Loader::loadClass("SelectListModel");
    	
    	$request = $this->getRequest();
    	$view = $this->view;
    	
    	//TODO　パラメターを取得
    	$code = $request->getParam("code");
    	$number_seat = $request->getParam("number_seat");
    	$description = $request->getParam("description");
    	$rent_price = $request->getParam("rent_price");
    	$type = $request->getParam("type");
    	
    	if($request->isPost()){
    		//TODO　会議室をデータベースに追加
    		$model = new RoomModel();
    		
    		$check = $model->addRoom($code, $type, $number_seat, $rent_price, $description);
    		
    		if($check){
    			$this->_redirect('/admin/room?msg=' . $code .'が追加されました。');
    		}else{
    			$this->_redirect('/admin/room?msg=' . $code .'が追加できません。');
    		} 
    	}
    	
    	//TODO　データをビューに送る
    	$view->code = $code;
    	$view->number_seat = $number_seat;
    	$view->type = $type;
    	$view->rent_price = $rent_price;
    	$view->description = $description;
    	$view->type = $type;
    	
    	$view->room_type_list = SelectListModel::getListRoomType();
    	$view->number_seat_list = SelectListModel::getListSeatNumberList();
    	
    	//TODO Navigator を設定
    	$nav['ホーム'] = "/admin";
    	$nav['会議室'] = "/admin/room";
    	$nav['追加'] = "/admin/room/add";
    	$view->nav = $nav;
    }
    
    /**
     * 会議室の情報を更新するためのアクション
     */
    public function editAction(){
    	//TODO　必要なクラスを読み込む
    	Zend_Loader::loadClass("RoomModel");
    	Zend_Loader::loadClass("SelectListModel");
    	
    	$request = $this->getRequest();
    	$view = $this->view;
    	
    	$edit_id = $request->getParam("edit_id");
    	
    	$model = new RoomModel();
    	
    	if(!$request->isPost()){
    		//TODO　更新したい会議室の情報をビューに送る
    		$editObject = $model->fetchRoom($edit_id);
    		
    		$view->edit_id = $editObject->id;
    		$view->code = $editObject->code;
    		$view->type = $editObject->type;
    		$view->number_seat = $editObject->number_seat;
    		$view->rent_price = $editObject->rent_price;
    		$view->description = $editObject->description;

    	}else{
    		//TODO　会議室情報を更新
			$data = array(
				'code' => $request->getParam("code"),
				'number_seat' => $request->getParam("number_seat"),
				'type' => $request->getParam("type"),
				'rent_price' => $request->getParam("rent_price"),
				'description' => $request->getParam("description")
			);    		
    		
			$check = $model->updateRoomData($edit_id, $data);
			
			if($check){
    			$this->_redirect('/admin/room?msg=' . $request->getParam("code") .'の情報が更新されました。');
    		}else{
    			$this->_redirect('/admin/room?msg=' . $request->getParam("code") .'の情報が更新できません。');
    		} 
    	}
    	
    	//TODO　必要データをビューに送る
    	$view->room_type_list = SelectListModel::getListRoomType();
    	$view->number_seat_list = SelectListModel::getListSeatNumberList();
    	
    	//TODO Navigator を設定
    	$nav['ホーム'] = "/admin";
    	$nav['会議室'] = "/admin/room";
    	$nav['会議室情報変更'] = "#";
    	$view->nav = $nav;
    }
    
	/**
	 * 会議室を削除するためのアクション
	 * AJAX で読む
	 */
    public function deleteAction ()
    {
        Zend_Loader::loadClass("RoomModel");

        $request = $this->getRequest();
        $model = new RoomModel();
        $del_ids = $request->getParam("del_ids");
        if ($del_ids) {
            if ($model->deleteRoom($del_ids))
                echo "success";
            else
                echo "error";
        } else {
            echo "not_found_id";
        }
        exit();
    }

    public function isRoomCodeExist ()
    {
    	Zend_Loader::loadClass("RoomModel");
    	
    	$request = $this->getRequest();
    	$view = $this->view;
    	
    	$model = new RoomModel();
    	if($request->getParam("code")){
    		echo "exist";	
    	}
    	
    	echo "not_exist";
    	
    	exit();
    }
}